package com.api.shopapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.MedicinerStaffRel;
import com.api.shopapi.entity.dto.MedicinerStaffRelDTO;
import com.api.shopapi.mapper.MedicinerStaffRelMapper;
import com.api.shopapi.service.MedicinerStaffRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("MedicinerStaffRelServiceImplOne")
public class MedicinerStaffRelServiceImpl implements MedicinerStaffRelService {

    @Autowired
    private MedicinerStaffRelMapper medicinerStaffRelMapper;


    @Override
    @Transactional
    public int insert(JSONObject jsonObject) {
        String medicinerCode = jsonObject.getString("medicinerCode");
        JSONArray medicinerStaffCodeArr = jsonObject.getJSONArray("medicinerStaffCodeArr");
        MedicinerStaffRel medicinerStaffRel = new MedicinerStaffRel();
        int count = 0;
        for (Object i : medicinerStaffCodeArr){
            medicinerStaffRel.setMedicinerStaffRelCode(UUID.randomUUID().toString());
            medicinerStaffRel.setMedicinerCode(medicinerCode);
            medicinerStaffRel.setStaffCode((String) i);
            medicinerStaffRelMapper.insert(medicinerStaffRel);
            count++;
        }
        return count;
    }

    @Override
    public
    int insert2(MedicinerStaffRel medicinerStaffRel){
        return medicinerStaffRelMapper.insert(medicinerStaffRel);
    }

    @Override
    public List<MedicinerStaffRelDTO> getListByMedicinerCode(String medicinerCode) {
        return medicinerStaffRelMapper.getListByMedicinerCode(medicinerCode);
    }

    @Override
    public int delByMedicinerCodeAndMedicinerStaffCode(String medicinerCode, String medicinerStaffCode) {
        return medicinerStaffRelMapper.delByMedicinerCodeAndMedicinerStaffCode(medicinerCode,medicinerStaffCode);
    }

    @Override
    public
    int delete(String medicinerStaffRelCode){
        return medicinerStaffRelMapper.delete(medicinerStaffRelCode);
    }

    @Override
    public MedicinerStaffRel getByStaffCode(String staffCode) {
        return medicinerStaffRelMapper.getByStaffCode(staffCode);
    }
}
